package com.hmapi.config;

import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
@Slf4j
public class DefaultFeignConfig {
    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }
    /**
     * OpenFeign之间调用传递信息
     * @return
     */
    @Bean
    public RequestInterceptor userInfoInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();
                log.info("OpenFeign 拦截器生效,userId:{}",userId);
                if(userId==null)
                {
                    return;
                }
                requestTemplate.header("user-info", String.valueOf(userId));
            }
        };
    }
}
